<?php
/***
 * 全名经纪人——登录
 */
namespace Api\Controller;
use Think\Controller;
class BrokersController extends Controller {
    public function index(){
        echo "o2o api v1.0";
    }
    //个人登录 
    public function authcodesun(){
        $user = I('user');
        $phone = I('phone');
        $code = I('code');
        $city_id =  I('cityid')?I('cityid'):15;
        //用户检查
        $map = array('agent_phone'=>$phone,'city_id'=>$city_id);
        if(M('freeagent3.t_agent')->where($map)->find()){
            exit('exits');
        }
        //验证码检查
        $map = array('phone'=>$phone);
        $varcode = M('freeagent3.t_authcode')->where($map)->field('authcode')->order('id desc')->limit(1)->find();
        if($code<>$varcode['authcode']){
            exit('errorcode');    
        }
        $data = array('agent_name'=>$user,'agent_phone'=>$phone,'open_id'=>'-1','create_time'=>date('Y-m-d H:i:s'),'city_id'=>$city_id);
        if(M('freeagent3.t_agent')->add($data)){
            $result = 'success';
        }else{
            $result = 'error';
        }
        exit($result);
    }
    //个人基本信息
    public function person(){
        $phone = I('phone');
        if(!$phone) exit(json_encode(array('code'=>0,'msg'=>"no p")));
        $city_id =  I('cityid')?I('cityid'):15;
        $map = array('agent_phone'=>$phone,'city_id'=>$city_id);
        if($agent = M('freeagent3.t_agent')->where($map)->field('agent_id,agent_name')->find()){
            $map = array('agent_id'=>$agent['agent_id'],'city_id'=>$city_id);
            $m= M('freeagent3.s_buy');
            $count = M('freeagent3.s_buy')->where($map)->count('buy_id');;  
            $map['status'] = 2;
            $deal_price = $m->where($map)->sum('deal_price');
            if($deal_price) $agent['commission'] = "已经赚佣金:".$deal_price."元";
            if($count) $agent['customer'] = "已推荐客户：".$count."人";
            exit(json_encode($agent));
        }else{
            exit(json_encode(array('code'=>0,'msg'=>"Unknown")));
        } 

    }
    //推荐房源
    public function recommend(){
        $phone = I('phone');//用户手机标识
        $houseid = I('check_val');
        $map_id['product_id']= array('in',$houseid);
        $house_info = M('freeagent3.s_project')->where($map_id)->select();
        if(!$phone) exit(json_encode(array('code'=>0,'msg'=>"no p")));
        $city_id =  I('cityid')?I('cityid'):15;
        $map = array('agent_phone'=>$phone,'city_id'=>$city_id);
        //是否已经推荐过
        $map2 = array_merge($map,array('rec_phone'=>I('tel')));
        $m = M('freeagent3.s_buy');
        if($w = $m->where($map2)->find()){
            exit(json_encode(array('code'=>0,'msg'=>'已经推荐过了')));    
        }
        if($agent = M('freeagent3.t_agent')->where($map)->field('agent_id')->find()){
            $house_name = '';
            foreach($house_info as $v){
                $buyinfo = array(
                    'agent_id'=>$agent['agent_id'],
                    'city_id'=>$city_id,
                    'rec_own'=>I('name'),
                    'rec_sex'=>I('sex'),
                    'rec_phone'=>I('tel'),
                    'product_id'=>$v['product_id'],
                    'deal_price'=>$v['deal_price']
                );
                //print_r($buyinfo);
                if(M('freeagent3.s_buy')->add($buyinfo)){
                    $house_name .= $v['project_name'].' ';
                } 
            } 
            $result = array('name'=>I('name'),'tel'=>I('tel'),'house'=>$house_name);
            exit(json_encode($result));
        }else{
            exit(json_encode(array('code'=>0,'msg'=>"Unknown")));
        } 

    }
    //我的客户
    public function mycustomer(){
        $phone = I('phone');
        $status = I('status');
        if(!$phone) exit(json_encode(array('code'=>0,'msg'=>"no p")));
        $city_id =  I('cityid')?I('cityid'):15;
        $map = array('agent_phone'=>$phone,'city_id'=>$city_id);
        if($agent = M('freeagent3.t_agent')->where($map)->field('agent_id,agent_name')->find()){
            $map2 = array('agent_id'=>$agent['agent_id'],'freeagent3.s_buy.city_id'=>$city_id,'freeagent3.s_buy.status'=>$status);
            $customer = M('freeagent3.s_buy')->join('freeagent3.s_project on freeagent3.s_buy.product_id=freeagent3.s_project.product_id')->field('rec_own,rec_phone,project_name')->where($map2)->select();
            exit(json_encode($customer));
        }else{
           exit(json_encode(array('code'=>0,'msg'=>"Unknown"))); 
        }
    }
    //我的佣金
    public function mycommission(){
        $phone = I('phone');
        if(!$phone) exit(json_encode(array('code'=>0,'msg'=>"no p")));
        $city_id =  I('cityid')?I('cityid'):15;
        $map = array('agent_phone'=>$phone,'city_id'=>$city_id);
        if($agent = M('freeagent3.t_agent')->where($map)->field('agent_id')->find()){
            $map = array('agent_id'=>$agent['agent_id'],'city_id'=>$city_id,'status'=>2);
            $m= M('freeagent3.s_buy');
            $count = M('freeagent3.s_buy')->where($map)->count('buy_id');;  
            $deal_price = $m->where($map)->sum('deal_price');
            if(!$deal_price&&!$count) exit(json_encode(array('code'=>0,'msg'=>"null"))); 
            else{
                $agent2['commission'] = $deal_price;
                $agent2['customer'] = $count;
                exit(json_encode($agent2));  
            }   
        }else{
           exit(json_encode(array('code'=>0,'msg'=>"Unknown"))); 
        }
    }
    public function my(){
        $phone = I('phone');
        if(!$phone) exit(json_encode(array('code'=>0,'msg'=>"no p")));
        $city_id =  I('cityid')?I('cityid'):15;
        $map = array('agent_phone'=>$phone,'city_id'=>$city_id);
        if($agent = M('freeagent3.t_agent')->where($map)->find()){
           
            exit(json_encode($agent));
        }else{
            exit(json_encode(array('code'=>0,'msg'=>"Unknown")));
        } 

    }
     public function edit(){
    
        $agent_id = I('agent_id');
        if(!$agent_id) exit(json_encode(array('code'=>0,'msg'=>"no p")));
        $city_id =  I('cityid')?I('cityid'):15;
        
    	$agent_name = I('agent_name');
    	$person_id=I('person_id');
    	$phone =I('phone');
    	$bank_name = I('bank_name');
    	$bank_no = I('bank_no');
    	$bank_owner=I('bank_owner');
    
    	//修改数据封装
    	if($agent_name||$person_id){
    		if($agent_name) $data['agent_name']=$agent_name;
    		if($person_id) $data['person_id']=$person_id;
    	}elseif($phone){
    		$data['phone']=$phone;
    	}elseif($bank_name||$bank_no||$bank_owner){
    		if($bank_name) $data['bank_name']=$bank_name;
    		if($bank_no) $data['bank_no']=$bank_no;
    		if($bank_owner) $data['bank_owner']=$bank_owner;
    	}else{
    		exit(json_encode(array('code'=>0,'msg'=>"Unknown")));
    	}
    	//print_r($data);
    	if(M('freeagent3.t_agent')->where(array('agent_id'=>$agent_id))->save($data)){
    		exit('success');
    	}else{
    		exit(json_encode(array('code'=>0,'msg'=>"uperror")));
    	}
    	 
    	
    }
    public function _empty(){
        //把所有城市的操作解析到city方法
         
    }
}